package My;

public class MyArrayList {
    private int[] arr;
    private int size;
    public MyArrayList(){
        arr = new int[10];
    }
    public MyArrayList(int capacity){
        arr = new int[capacity];//通过 new 时传入参数 ， 指定容量
    }

    public int size(){
        return size;
    }

    public void add(int val){
        if (size == arr.length){
            resize();
        }
        arr[size] = val;
        size++;
    }
    private void resize(){
        int[] newArr = new int[(int)(arr.length*1.5)];
        for (int i = 0; i < arr.length; i++) {
            newArr[i] = arr[i];
        }
        arr = newArr;
    }

    public String toString(){
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("[");
        for (int i = 0; i < size; i++) {
            stringBuilder.append(arr[i]);
            if (i<size){
//                System.out.println(",");
                stringBuilder.append(",");
            }
        }
        stringBuilder.append("]");
        return stringBuilder.toString();
    }
    public static void main(String[] args) {
        MyArrayList list = new MyArrayList();
        list.add(1);
        System.out.println(list.size());
        System.out.println(list.toString());
    }
}
